package com.kidedu.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.ibatis.session.RowBounds;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.struts2.ServletActionContext;

import com.kidedu.dao.mapper.KStudentMapper;
import com.kidedu.entity.KStudent;
import com.kidedu.entity.KStudentExample;
import com.kidedu.entity.dto.KStudentDTO;
import com.kidedu.service.IKStudentService;
import com.kidedu.util.BaseService;

public class KStudentService extends BaseService implements IKStudentService{
	
	@Resource
	KStudentMapper kStudentMapper;
	public JSONObject BatchImport(Sheet sheet) {
		// TODO Auto-generated method stub
		JSONObject json=new JSONObject();
		int total=sheet.getLastRowNum();
		 Row row=null;
		 KStudent stu;
		 for(int i=0;i<=total;i++)
			{
				int j=-1;
				row=sheet.getRow(i);
				stu=new KStudent();
				row=sheet.getRow(i);
			
				 try{
					 row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
			         stu.setSid(row.getCell(0).getRichStringCellValue().toString());
					 
					 row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
			         stu.setSusername(row.getCell(1).getRichStringCellValue().toString());
			       
			    	 row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
			         stu.setSsex(Byte.valueOf(row.getCell(2).getRichStringCellValue().toString()));
			        
			     	 row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);			   			     
				     stu.setSbeginyear(row.getCell(3).getStringCellValue());
				    
					 row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
				     stu.setSbegingrade(row.getCell(4).getRichStringCellValue().toString());
				     
					 row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
				     stu.setSclass(row.getCell(5).getRichStringCellValue().toString());
				   
					 row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
				     stu.setSparentname(row.getCell(6).getRichStringCellValue().toString());	
				     row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
				 	
				     BigDecimal  bd = new BigDecimal(row.getCell(7).getRichStringCellValue().getString());  
				     stu.setSparenttel(bd.toPlainString());
	                  stu.setSpassword("123456");
			           j= kStudentMapper.insertSelective(stu);
				   if(j==-1)
				   {	
					   json.put("error", "第"+i+"条数据格式有误！");
				   }
				   }
				 catch (Exception e)
				 {
					   json.put("error2", "第"+i+"条数据格式有误！");
				 }
			
			}
		 json.put("message", "导入成功");
		return json;
	}
	public boolean Login(String username, String password) {
		// TODO Auto-generated method stub
	   
	
		  KStudentExample kstudentExample=new KStudentExample();
		  kstudentExample.createCriteria().andSusernameEqualTo(username).andSpasswordEqualTo(password);
		 
		  
		  try
		  {
			  KStudent student=kStudentMapper.selectByExample(kstudentExample).get(0);
			  if(student!=null)
				{
					HttpServletRequest req = ServletActionContext.getRequest();
					req.getSession().setAttribute("STUDENT", student);
				  return true;
				}
				
		  }
		  catch(Exception e)
		  {
			  
		  }
		 
		  
		return false;
	}
	public JSONObject StudentList(int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		 JSONObject json=new JSONObject();
		  KStudentExample kstudentExample=new KStudentExample();
	 List<KStudent> studentList=kStudentMapper.selectByExample(kstudentExample,  new RowBounds(super.calculateStartPage(pageNo, pageSize), pageSize));
	//	 List<KStudent> studentList=kStudentMapper.selectByExample(kstudentExample);
		 int count=0;
		 if(studentList!=null)
		 {
			 count=kStudentMapper.selectByExample(kstudentExample).size();
			 List<KStudentDTO> studentDTOList=new ArrayList<KStudentDTO>();
			 for(KStudent student : studentList)
			 {
				 KStudentDTO studentDTO=new KStudentDTO();
				 studentDTO.setSid(student.getSid());
				 studentDTO.setUsername(student.getSusername());
				 studentDTO.setsSex(student.getSsex());
				 studentDTO.setsBegingrade(student.getSbegingrade());
				 studentDTO.setsBeginyear(student.getSbeginyear());
				 studentDTO.setsParentname(student.getSparentname());
				 studentDTO.setsParenttel(student.getSparenttel());
				 studentDTOList.add(studentDTO);
			 }
			 json.put("StudentList", studentDTOList);

		 }
		 json.put("count", count);
	
		 
		return json;
	}
	public boolean AddSingleStudent(KStudent student) {
		// TODO Auto-generated method stub
		try{
			if(kStudentMapper.insertSelective(student)!=-1)
			    return true;
		}catch(Exception e)
		{
			return false;
		}
	
		return false;
	}
	public boolean deleteStudent(List<String> StudentIdList) {
		// TODO Auto-generated method stub
		  KStudentExample kstudentExample=new KStudentExample();
		  kstudentExample.createCriteria().andSidIn(StudentIdList);
		  try{
			  kStudentMapper.deleteByExample(kstudentExample);
			  return true;
		  }catch(Exception e)
		  {
				return false;
		  }
	
	
	}
	public KStudent selectSingleStudent(String Stu_id) {
		// TODO Auto-generated method stub
		  KStudentExample kstudentExample=new KStudentExample();
		  kstudentExample.createCriteria().andSidEqualTo(Stu_id);
		return kStudentMapper.selectByExample(kstudentExample).get(0);
	}
	@Override
	public boolean updateSingleStudent(KStudent student) {
		// TODO Auto-generated method stub
		
		kStudentMapper.updateByPrimaryKeySelective(student);
		return true;
	}
	@Override
	public boolean deleteSingleStudent(String sid) {
		// TODO Auto-generated method stub
		kStudentMapper.deleteByPrimaryKey(sid);
		
		return true;
	}
}
